﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.BaoCao
{
    public partial class LienHeQuanTri : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Pager1.CurrentIndex = 1;
                BindLienHe(Pager1.CurrentIndex);
            }
        }

        private void BindLienHe(int PageNo)
        {
            string WhereClause = "";
            WhereClause += "MaCanBo=" + Common.GetKeyUserInfo("Id");
            if (chkTrangThai.Checked == true)
            {
                WhereClause += " and TrangThai=0";
            }
            int TotalRows = 0;
            DataSet dsLienHe = DataRepository.Provider.ExecuteDataSet("LienHe_GetPaged", WhereClause, "NgayCapNhat DESC",
                PageNo - 1, Pager1.PageSize);
            rpLienHe.DataSource = dsLienHe.Tables[0];
            rpLienHe.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsLienHe.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            BindLienHe(currnetPageIndex);
        }


        protected void cmdSend_OnClick(object sender, EventArgs e)
        {
            bool status = false;
            LienHe ent_LienHe = new LienHe();
            ent_LienHe.TieuDe = txtTieuDe.Text;
            ent_LienHe.NoiDung = txtNoiDung.Text;
            ent_LienHe.NgayCapNhat = Common.GetServerDateTime();
            ent_LienHe.TrangThai = false;
            ent_LienHe.MaCanBo = Common.GetKeyUserInfo("Id");
            status = DataRepository.LienHeProvider.Insert(ent_LienHe);
            if (!status)
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
                txtTieuDe.Text = "";
                txtNoiDung.Text = "";
                BindLienHe(Pager1.CurrentIndex);
            }
        }

        protected void chkTrangThai_OnCheckedChanged(object sender, EventArgs e)
        {
            BindLienHe(Pager1.CurrentIndex);
        }

        public string getPhanHoi(object ob)
        {
            string id = ob.ToString();
            GiaiDap ent_GiaiDap = DataRepository.GiaiDapProvider.GetByMaLienHe(Convert.ToInt32(id));
            if (ent_GiaiDap != null)
                return ent_GiaiDap.NoiDung;
            else
            {
                return "";
            }

        }
    }
}